package com.example.demo.store.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.store.entity.StorageTbl;
import com.example.demo.store.feign.IAccountFeign;
import com.example.demo.store.mapper.StorageTblMapper;
import com.example.demo.store.service.IStorageTblService;
import com.netflix.discovery.converters.Auto;
import io.seata.core.context.RootContext;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zgq
 * @since 2020-12-07
 */
@Slf4j
@Service
public class StorageTblServiceImpl extends ServiceImpl<StorageTblMapper, StorageTbl> implements IStorageTblService {


    @Autowired
    StorageTblMapper storageTblMapper;


    @Autowired
    IAccountFeign accountFeign;


    @GlobalTransactional(rollbackFor = Exception.class)
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int dec(String userId,int amt) {
        log.error("开始扣库存--------------");
        log.info("StorageTblServiceImpl->dec:    amt:{}   xid:{}",amt, RootContext.getXID());
        StorageTbl storageTbl = storageTblMapper.selectById(1);

        storageTbl.setCount(storageTbl.getCount()-amt);
        log.error("扣storage--------------");
        int ret = storageTblMapper.updateById(storageTbl);
        log.error("扣account--------------");
        ret = accountFeign.decAccount(userId,amt);

        log.error("结束扣库存--------------");
        return ret;

    }
}
